home *** CD-ROM | disk | FTP | other *** search
/ Magnum One / Magnum One (Mid-American Digital) (Disc Manufacturing).iso / d12 / pj64.arc / DCINIT.C < prev    next >
Text File  |  1988-12-16  |  8KB  |  235 lines

  1. /* 
  2.   Initialization segment for the digital clock.
  3.   This code can be discarded after it is used.
  4.  */
  5.  
  6. #include <windows.h>
  7. #include "dc.h"
  8.  
  9. /* local function declarations */
  10. static BOOL NEAR RegisterWindowClass(HANDLE);
  11. static HFONT NEAR GetFontInfo(HANDLE);
  12. static void NEAR GetPrevInstanceData(HANDLE);
  13. static BOOL NEAR MakeAndShowMainWnd(HANDLE, HANDLE, int);
  14.  
  15. /* This routine is FAR since it is called from another segment */
  16. BOOL FAR InitProgram(hInstance,hPrevInstance, lpszCmdLine, cmdShow)
  17. HANDLE hInstance, hPrevInstance;
  18. LPSTR lpszCmdLine;
  19. int cmdShow;
  20. {
  21.  
  22.   /* if this is the first instance of the program ... */
  23.     if (!hPrevInstance) {
  24.       /* read some strings from the resources */
  25.     LoadString(hInstance,IDS_TIMESTR,(LPSTR)szTimestr,sizeof(szTimestr));
  26.     LoadString(hInstance,IDS_AM,(LPSTR)szAM,sizeof(szAM));
  27.     LoadString(hInstance,IDS_PM,(LPSTR)szPM,sizeof(szPM));
  28.       /* register the window */
  29.     if (!RegisterWindowClass(hInstance))
  30.         return FALSE;
  31.       /* get a font for displaying the time in the icon window */
  32.         if ((hFont = GetFontInfo(hInstance)) == NULL)
  33.         return FALSE;
  34.     }
  35.   /* A previous instance already exists so get global data from there */
  36.     else
  37.     GetPrevInstanceData(hPrevInstance);
  38.  
  39.   /* Create and show the window */
  40.     if (!MakeAndShowMainWnd(hInstance,hPrevInstance, cmdShow))
  41.     return FALSE;
  42.  
  43.   /* Set the timer to generate a WM_TIMER message about once a second */
  44.     if (!(ClockTimerEventID = SetTimer(hWndMain, NULL, 975, NULL)))
  45.         /* at 1000 msec, clock skips a second every 24 seconds;   */
  46.         /* at  975 msec, clock repeats a second every 87 seconds  */
  47.         return FALSE;
  48.  
  49.     return TRUE;
  50. }
  51.  
  52. /* Every window must belong to a class. We register ours here */
  53. static BOOL NEAR RegisterWindowClass(hInstance)
  54. HANDLE hInstance;
  55. {
  56.  
  57.     PWNDCLASS pWndClass;
  58.     HANDLE hTemp;
  59.  
  60.   /* Load the name string from resources */
  61.     LoadString(hInstance, IDS_APPNAME,(LPSTR)szAppName,sizeof(szAppName));
  62.  
  63.   /* allocate space for the WNDCLASS structure and lock it down */
  64.     hTemp = LocalAlloc(LPTR,sizeof(WNDCLASS));
  65.     pWndClass = (PWNDCLASS)LocalLock(hTemp);
  66.  
  67.   /* fill the structure */    
  68.     pWndClass->hCursor    = LoadCursor(NULL, IDC_ARROW);  /* standard cursor */
  69.     pWndClass->hIcon    = NULL;                /* no icon */
  70.     pWndClass->lpszMenuName = NULL;            /* no class menu */
  71.     pWndClass->lpszClassName = (LPSTR)szAppName;    /* our class name */
  72.     pWndClass->hbrBackground = GetStockObject(WHITE_BRUSH); /*white background*/
  73.     pWndClass->hInstance = hInstance;        /* instance handle */
  74.     pWndClass->style = CS_VREDRAW | CS_HREDRAW; /* standard redraw values */
  75.     pWndClass->lpfnWndProc = MainWndProc;    /* pointer to our window proc */
  76.  
  77.   /* register the class.  if fail, abort */
  78.     if (!RegisterClass((LPWNDCLASS)pWndClass))
  79.     return FALSE;
  80.  
  81.   /* free the memory used */
  82.     LocalUnlock(hTemp);
  83.     LocalFree(hTemp);
  84.  
  85.   /* show success */
  86.     return TRUE;
  87. }
  88.  
  89. /*
  90.    If this not the first instance, we can retrieve static data from a
  91.    previous invocation of the program
  92. */
  93. static void NEAR GetPrevInstanceData(hInstance)
  94. HANDLE hInstance;
  95. {
  96.  
  97.     GetInstanceData(hInstance, (PSTR)szAppName, sizeof(szAppName));
  98.     GetInstanceData(hInstance, (PSTR)szTimestr, sizeof(szTimestr));
  99.     GetInstanceData(hInstance, (PSTR)szAM, sizeof(szAM));
  100.     GetInstanceData(hInstance, (PSTR)szPM, sizeof(szPM));
  101.     GetInstanceData(hInstance, (PSTR)&hFont, sizeof(HFONT));
  102. }
  103.  
  104. /*
  105.  Create the window, making sure that its position and size are suitable
  106.  for the display.
  107. */
  108. static BOOL NEAR MakeAndShowMainWnd(hInstance, hPrevInstance, cmdShow)
  109. HANDLE hInstance;
  110. HANDLE hPrevInstance;
  111. int cmdShow;
  112. {
  113.  
  114.     DWORD style;
  115.     RECT clrect;
  116.     HDC hIC;
  117.     char szDisplayName[20];
  118.     TEXTMETRIC TM;
  119.     int displaywidth, displayheight;
  120.     int width, height;
  121.  
  122.   /* First, create an information context for the display */
  123.     LoadString(hInstance, IDS_DISPLAYNAME,
  124.              (LPSTR)szDisplayName,sizeof(szDisplayName));
  125.     hIC = CreateIC((LPSTR)szDisplayName,(LPSTR)NULL, (LPSTR)NULL, (LPSTR)NULL);
  126.   /* Now get the size of the system font */
  127.     GetTextMetrics(hIC, (LPTEXTMETRIC)&TM);
  128.   /* Also, retrieve information about the width and height of the display */
  129.     displayheight = GetDeviceCaps(hIC, VERTRES);
  130.     displaywidth = GetDeviceCaps(hIC, HORZRES);
  131.   /* delete what we no longer need */
  132.     DeleteDC(hIC);
  133.   /* 
  134.      Now we specify the client window we would like to have.  However,
  135.      its position is only nominal and later we will move it.  What we
  136.      really want is the size of the window we need to create to achieve
  137.      the desired client area.  Since the display is one line, the client
  138.      must be high enough to display it.  And, since the text is an
  139.      expression of the form hh:mm:ss A.(P.)M., it must be at least 13
  140.      characters wide.
  141.    */
  142.     clrect.left = 0;
  143.     clrect.top = 0;
  144.     clrect.right = TM.tmAveCharWidth * 13;
  145.     clrect.bottom = TM.tmHeight + TM.tmExternalLeading;
  146.  
  147.   /* this is the style we want */
  148.     style = WS_OVERLAPPED | WS_MINIMIZEBOX | WS_CAPTION | WS_SYSMENU
  149.                 | WS_THICKFRAME;
  150.   /* adjust the rectangle to the actual window size */
  151.     AdjustWindowRect((LPRECT)&clrect, style, FALSE);
  152.   /* now compute the actual client width and height */
  153.     width = clrect.right - clrect.left;
  154.     height = clrect.bottom - clrect.top;
  155.   /* create the window, letting it fall where Windows wants */
  156.     hWndMain = CreateWindow((LPSTR)szAppName,
  157.                  (LPSTR)szAppName,
  158.                  style,
  159.                  CW_USEDEFAULT,0,
  160.                  width, height,
  161.                  (HWND)NULL,
  162.                  (HMENU)NULL,
  163.                  (HANDLE)hInstance,
  164.                  (LPSTR)NULL);
  165.  
  166.   /* if we fail, give up */
  167.     if (hWndMain == NULL)
  168.     return FALSE;
  169.   /* 
  170.      if this is the first instance, we move the window
  171.      to the lower right corner.
  172.   */
  173.     if (!hPrevInstance) {
  174.       /* first, find out where we are */
  175.         GetWindowRect(hWndMain, (LPRECT)&clrect);
  176.       /* compute the width and height */
  177.     width = clrect.right - clrect.left;
  178.     height = clrect.bottom - clrect.top;
  179.       /* now move the window down to the right corner */
  180.         MoveWindow(hWndMain, displaywidth - width, displayheight - height,
  181.              width, height,FALSE);
  182.     }
  183.   /* finally, display the window and show success */
  184.     ShowWindow(hWndMain, cmdShow);
  185.     UpdateWindow(hWndMain);
  186.  
  187.     return TRUE;
  188. }
  189.  
  190. /* 
  191.    Create a logical font that is suitable for displaying the time
  192.    in our format when the window is iconic.  Since the font weight
  193.    and size will depend on the display in use, three of the parameters
  194.    can be read from WIN.INI.  For CGA type displays, try using a
  195.    height of 6, a width of 4, and a weight of 400.  For the EGA,
  196.    values of 8, 6, and 700 (the defaults) are satisfactory.
  197. */
  198. static HFONT NEAR GetFontInfo(hInstance)
  199. HANDLE hInstance;
  200. {
  201.  
  202.     char szKeystr[30];
  203.   /* fill this structure with the font characteristics we want */
  204.     LOGFONT clockfont;  
  205.     HFONT htemp;
  206.   /* read the height from Win.ini */
  207.     LoadString(hInstance,IDS_FONTHEIGHT,(LPSTR)szKeystr, sizeof(szKeystr));
  208.     clockfont.lfHeight = GetProfileInt((LPSTR)szAppName, (LPSTR)szKeystr,8);
  209.  
  210.   /* read the width from Win.ini */
  211.     LoadString(hInstance,IDS_FONTWIDTH,(LPSTR)szKeystr, sizeof(szKeystr));
  212.     clockfont.lfWidth = GetProfileInt((LPSTR)szAppName, (LPSTR)szKeystr,6);
  213.  
  214.   /* read the weight from Win.ini */
  215.     LoadString(hInstance,IDS_FONTWEIGHT,(LPSTR)szKeystr, sizeof(szKeystr));
  216.     clockfont.lfWeight = GetProfileInt((LPSTR)szAppName, (LPSTR)szKeystr,700);
  217.  
  218.   /* default these values */
  219.     clockfont.lfEscapement = 0;
  220.     clockfont.lfOrientation = 0;
  221.     clockfont.lfItalic = 0;
  222.     clockfont.lfUnderline = 0;
  223.     clockfont.lfStrikeOut = 0;
  224.     clockfont.lfCharSet = ANSI_CHARSET;
  225.     clockfont.lfOutPrecision = OUT_DEFAULT_PRECIS;
  226.     clockfont.lfClipPrecision = CLIP_DEFAULT_PRECIS;
  227.     clockfont.lfQuality = DEFAULT_QUALITY;
  228.     clockfont.lfPitchAndFamily = FIXED_PITCH | FF_MODERN;
  229.     clockfont.lfFaceName[0] = 0;
  230.  
  231.   /* get the handle to the font */
  232.  
  233.     return(CreateFontIndirect((LPLOGFONT)&clockfont));
  234. }
  235.